package com.trainingcloud;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.dom4j.Element;

import com.trainingcloud.entity.ClientDocument;
import com.trainingcloud.entity.Role;
import com.trainingcloud.entity.User;

public class XMLSerializer 
{
	private static String dateToString(Date date)
	{
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(date);
	}
	
	public static void userToXML(User user, Element parent)
	{
		userToXML(user, parent, null);
	}
	
	public static void userToXML(User user, Element parent, String alias)
	{
		Element userElement = parent.addElement("user");
		if(alias!=null)
			userElement.setName(alias);
		
		userElement.addElement("id")
			.addText(String.valueOf(user.getId()));
		
		userElement.addElement("loginName")
		   .addText(String.valueOf(user.getLoginName()));
		
		userElement.addElement("loginPassword")
		   .addText(String.valueOf(user.getLoginPassword()));
		
		userElement.addElement("name")
		   .addText(String.valueOf(user.getName()));
		
		userElement.addElement("email")
		   .addText(String.valueOf(user.getEmail()));
		
		roleToXML(user.getRole(), userElement);
		
		userElement.addElement("active")
		   .addText(String.valueOf(String.valueOf(user.isActive())));
		
		userElement.addElement("updateTime")
		   .addText(dateToString(user.getUpdateTime()));
		
		userElement.addElement("createTime")
		   .addText(dateToString(user.getCreateTime()));
		
		
		if(user.getLatestLoginTime()!=null)
		{
			userElement.addElement("latestLoginTime")
			   .addText(dateToString(user.getLatestLoginTime()));
		}
	}
	
	public static void roleToXML(Role role, Element parent)
	{
		roleToXML(role, parent, null);
	}
	
	public static void roleToXML(Role role, Element parent, String alias)
	{
		Element roleElement = parent.addElement("role");
		if(alias!=null)
			roleElement.setName(alias);
		
		roleElement.addElement("id")
			.addText(String.valueOf(role.getId()));
	
		roleElement.addElement("name")
			.addText(role.getName());
		
		roleElement.addElement("userManagement")
			.addText(String.valueOf(role.isUserManagement()));
		
		roleElement.addElement("systemBackup")
			.addText(String.valueOf(role.isSystemBackup()));
		
		roleElement.addElement("systemRestore")
			.addText(String.valueOf(role.isSystemRestore()));
		
		roleElement.addElement("documentManagement")
			.addText(String.valueOf(role.isDocumentManagement()));
		
		roleElement.addElement("documentShare")
			.addText(String.valueOf(role.isDocumentShare()));
	}
	
	public static void clientDocumentToXML(ClientDocument document, Element parent)
	{
		clientDocumentToXML(document, parent, null);
	}

	public static void clientDocumentToXML(ClientDocument document, Element parent, String alias)
	{
		Element docElement = parent.addElement("document");
		if(alias!=null)
			docElement.setName(alias);
		
		docElement.addElement("id")
			.addText(String.valueOf(document.getDocument().getId()));
	
		docElement.addElement("title")
			.addText(document.getDocument().getTitle());
		
		docElement.addElement("filename")
			.addText(document.getDocument().getFilename());
		
		docElement.addElement("mimeType")
			.addText(document.getDocument().getMimeType());
		
		docElement.addElement("shared")
			.addText(String.valueOf(document.getDocument().isShared()));
		
		docElement.addElement("createTime")
			.addText(dateToString(document.getDocument().getCreateTime()));
		
		userToXML(document.getDocument().getOwner(), docElement, "owner");
		
		if(document.getDocument().getSharedTime()!=null)
		{
			docElement.addElement("sharedTime")
				.addText(dateToString(document.getDocument().getSharedTime()));
		}
		
		docElement.addElement("favorited")
			.addText(String.valueOf(document.isFavorite()));
		
		if(document.getLatestUsedTime()!=null)
		{
			docElement.addElement("latestUsedTime")
				.addText(dateToString(document.getLatestUsedTime()));
		}
	}
}
